<?php

namespace common;

class helper
{
    private $zone;
    private $startTime;
    private $endTime;
    //根据站点获取时区
    public function getTimeZone($site)
    {
        switch ($site) { #开始时间加86400  结束时间加 2*86400
            case 'US':
            case 'CA':
            case 'MX':
                $this->zone = 'America/Los_Angeles';
                $this->startTime = 86400;
                $this->endTime = 172800;
                break;
            case 'GB':
            case 'UK':
            case 'FR':
            case 'DE':
            case 'IT':
            case 'ES':
            case 'NL':
            case 'SE':
                $this->zone = 'UTC';#开始时间加7小时 结束时间加 86400 + 7 * 3600
                $this->startTime = 25200;
                $this->endTime = 111600;
                break;
            case 'JP':
                $this->zone = 'Asia/Tokyo';
                break;
            case 'AU':
                $this->zone = 'Australia/Sydney';
                break;
            case 'IN':
                $this->zone = 'Indian/Mayotte';
                break;
            default:
                $this->zone = 'PRC';
        }
    }

    public static function strReplaceDate($Site, $Date)
    {
        $Date = trim($Date);
        $ReplaceList = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
        switch ($Site) {
            case 'MX':
                $FindList = array('enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre');
                $Date = str_ireplace($FindList, $ReplaceList, $Date);
                break;
            case 'IT':
                $FindList = array('gen', 'feb', 'mar', 'apr', 'mag', 'giu', 'lug', 'ago', 'set', 'ott', 'nov', 'dic');
                $Date = str_ireplace($FindList, $ReplaceList, $Date);
                $Date = str_ireplace('.', ':', $Date);
                $Date = self::formatDate($Date);
                break;
            case 'DE':
                $Date = str_ireplace('.', '-', $Date);
                $Date = self::formatTransactionDate($Date);
                break;
            case 'FR':
                $FindList = array('janv.', 'Févr.', 'mars', 'avr.', 'mai', 'juin', 'juil.', 'août', 'sept.', 'oct.', 'nov.', 'déc.');
                $Date = str_ireplace($FindList, $ReplaceList, $Date);
                break;
            case 'ES':
                $FindList = array('ene.', 'feb.', 'mar.', 'abr.', 'may.', 'jun.', 'jul.', 'ago.', 'sep.', 'oct.', 'nov.', 'dic.');
                $Date = str_ireplace($FindList, $ReplaceList, $Date);
                break;
            case 'JP':
            case 'AU':
                $Date = self::formatTransactionDate($Date);
                break;
            default:
                break;
        }
        $Date = trim($Date);
        $Time = strtotime($Date);
        return empty($Time) ? 0 : $Time;
    }

    public static function formatDate($open_time)
    {
        $is_open_time = strpos($open_time, '/');
        if ($is_open_time !== false) {
            $open_time_array = explode(' ', $open_time);
            if (!empty($open_time_array[0])) {
                $open_time_list = explode('/', $open_time_array[0]);
                $open_time_array[0] = implode('-', array_reverse($open_time_list));
            }
            if (!empty($open_time_array[2])) {
                if ($open_time_array[2] == 'MEST') {
                    $open_time_array[2] = 'MET';
                }
            }
            $open_time = implode(' ', $open_time_array);
        }
        return $open_time;
    }

    public static function formatTransactionDate($open_time)
    {
        $is_open_time = strpos($open_time, '/');
        if ($is_open_time !== false) {
            $open_time_array = explode(' ', $open_time);
            if (!empty($open_time_array[0])) {
                $open_time_list = explode('/', $open_time_array[0]);
                $open_time_array[0] = implode('-', array_reverse($open_time_list));
            }
            if (!empty($open_time_array[2])) {
                if ($open_time_array[2] == 'MEST') {
                    $open_time_array[2] = 'MET';
                }
            }
            $open_time = implode(' ', $open_time_array);
        }
        return $open_time;
    }


    public function startformatTime($site ,$date, $from_tz = 'PRC', $fm = 'Y-m-d\TH:i:s+00:00')
    {
        $this->getTimeZone($site);
        $date = date('Y-m-d H:i:s', strtotime($date) + $this->startTime);
        $datetime = new \DateTime($date, new \DateTimeZone($from_tz));
        $datetime->setTimezone(new \DateTimeZone($this->zone));
        return $datetime->format($fm);
    }

    public function endformatTime($site ,$date, $from_tz = 'PRC', $fm = 'Y-m-d\TH:i:s+00:00')
    {
        $this->getTimeZone($site);
        $date = date('Y-m-d H:i:s', strtotime($date) + $this->endTime);
        $datetime = new \DateTime($date, new \DateTimeZone($from_tz));
        $datetime->setTimezone(new \DateTimeZone($this->zone));
        return $datetime->format($fm);
    }


    /**
     * 获取月度时间
     */
    public static function getSiteMorthDateList($Site, $MonthNum, $openTime = 0, $MonthStartDay = '01', $MonthEndDay = 't')
    {
        if (empty($openTime)) {
            $openTime = strtotime(date('Y-m-15', time()));
        }
        $timezone = date('e'); //获取当前使用的时区
        date_default_timezone_set('PRC'); //修改时区
        $dataTime = strtotime("{$MonthNum} month", $openTime);
        $startTimestamp = strtotime(date("Y-m-{$MonthStartDay} 00:00:00", $dataTime));
        $endTimestamp = strtotime(date("Y-m-{$MonthEndDay} 23:59:59", $dataTime));
        $list = [
            'monthId' => date('Y-m', $dataTime),
            'yearNum' => date('Y', $dataTime),
            'monthNum' => date('m', $dataTime),
            'startTime' => $startTimestamp,
            'endTime' => $startTimestamp,
            'formatStartTime' => date('Y-m-d\TH:i:s+00:00', $startTimestamp),
            'formatEndTime' => date('Y-m-d\TH:i:s+00:00', $endTimestamp),
            'startDate' => date("Y-m-{$MonthStartDay}", $dataTime),
            'endDate' => date("Y-m-{$MonthEndDay}", $dataTime)
        ];
        date_default_timezone_set($timezone); //还原时区
        return $list;
    }
}
